<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channels, ind) in myChannels"
        :key="ind"
        @click="onMyChannelClick(channels, ind)"
      >
        <van-icon
          v-show="isEdit && !fiexdChannels.includes(ind)"
          slot="icon"
          name="clear"
        ></van-icon>
        <span class="text" :class="{ active: active === ind }" slot="text">{{
          channels.name
        }}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        icon="plus"
        v-for="(obj, ind) in recommentChannels"
        :key="ind"
        @click="onAddChannel(obj)"
      >
        <div slot="text">{{ obj.name }}</div>
      </van-grid-item>
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannels,
  deleteUserChannels
} from '@/api/channel'
import { setItem } from '@/utils/storage'
import { mapState } from 'vuex'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      AllChannels: [], // 保存所有频道
      isEdit: false, // 默认是非编辑状态
      fiexdChannels: [0]
    }
  },
  computed: {
    ...mapState(['user']),
    recommentChannels () {
      const arr = []
      this.AllChannels.forEach((channel) => {
        const ret = this.myChannels.find((myChannel) => {
          return myChannel.id === channel.id
        })

        if (!ret) {
          arr.push(channel)
        }
      })
      return arr
    }
  },
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    async loadAllChannels () {
      try {
        const res = await getAllChannels()
        this.AllChannels = res.data.data.channels
        console.log(this.AllChannels)
      } catch (error) {
        this.$toast('获取所有的频道失败')
      }
    },

    onMyChannelClick (channel, ind) {
      if (this.isEdit) {
        if (this.fiexdChannels.includes(channel.id)) {
          return
        }
        this.myChannels.splice(ind, 1)
        if (ind < this.active) {
          // 不能再子组件 直接修改父组件的数据
          this.$emit('uodata-active', this.active - 1, true)
        }
        if (ind === this.active) {
          this.$emit('uodata-active', 0, true)
        }
        this.deleteChannel(channel)
      } else {
        this.$emit('uodata-active', ind, false)
      }
    },

    // 添加用户频道
    async onAddChannel (channel) {
      this.myChannels.push(channel)
      // 判断有没有登录
      if (this.user) {
        // 已登录
        try {
          await addUserChannels({
            id: channel.id,
            seq: this.myChannels.length
          })
          console.log('添加成功')
        } catch (error) {
          console.log(error)
        }
      } else {
        // 未登录
        setItem('totiao_KEY', this.myChannels)
      }
    },

    // 删除用户频道
    async deleteChannel (channel) {
      if (this.user) {
        // 登录
        try {
          await deleteUserChannels(channel.id)
          this.$toast('删除成功')
        } catch (error) {
          this.$toast('删除失败')
        }
      } else {
        // 未登录
        setItem('totiao_KEY', this.myChannels)
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
